package com.yazi.mxz.admin.controller;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.myframework.smhj.common.exception.MyException;
import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Setting;
import com.yazi.mxz.common.entity.enums.SettingStatus;
import com.yazi.mxz.common.entity.enums.SettingType;
import com.yazi.mxz.common.service.SettingService;

@Controller("settingController")
@RequestMapping("/setting")
public class SettingController extends BaseController {
	private Logger log = LoggerFactory.getLogger(SettingController.class);
	
	@Resource
	SettingService settingService;
	
	@RequestMapping("/list")
	public String list(Model model){
		log.debug("进入设置页面");
		List<Setting> setList = settingService.listAll();
		SettingStatus[] statuses = SettingStatus.values();
		SettingType[] types = SettingType.values();
		model.addAttribute("setList", setList);
		model.addAttribute("statuses", statuses);
		model.addAttribute("types", types);
		return "setting_list";
	}
	
	@RequestMapping("/edit")
	public String edit(Integer settingId, Model model){
		log.debug("进入修改设置页面");
		Setting set = settingService.one(settingId);
		SettingStatus[] statuses = SettingStatus.values();
		SettingType[] types = SettingType.values();
		model.addAttribute("statuses", statuses);
		model.addAttribute("types", types);
		model.addAttribute("setting", set);
		return "setting_edit";
	}
	
	@RequestMapping("/editDo")
	public String editDo(Setting set){
		log.debug("执行修改设置操作");
		try {
			settingService.edit(set);
		} catch (MyException e) {
			log.debug("添加/编辑角色失败: {}", e.getMessage());
		}
		return "redirect:/setting/list";
	}
}
